clear
use data_working

// DATA PREP

* Limit years:

drop if year==1947
drop if year>1975

* Generate per capita beds:

gen popul=ipop/1000
replace popul=0 if popul==.

gen bedstot_temps=beds_tot
  replace bedstot_temps=0 if beds_tot==1
gen bedsnppub_temps=beds_nppub
  replace bedsnppub_temps=0 if beds_nppub==1
gen bedsfp_temps=bdtotfp
  replace bedsfp_temps=0 if bdtotfp==1

gen bdpc=bedstot_temps/(popul+1)
gen bdpc_nppub=bedsnppub_temps/(popul+1)
gen bdpc_fp=bedsfp_temps/(popul+1)

gen admtotpc=admtot/(popul+1)
gen admnppubpc=adm_nppub/(popul+1)
gen admfppc=admtotfp/(popul+1)

* Treatment indicators and lags (any project approval):

xtset fcounty year

* drop treat_lag* (original do file previously created first treatment indicators)

gen treat_lag=treat_it
  forvalues y=1/20  {
	gen byte treat_lag`y' = L`y'.treat_lag 
	replace treat_lag`y' = 0 if treat_lag`y' ==.
  }

gen byte treat_lag21 = 0
  forvalues y=21/30  {
 	replace treat_lag21 = 1 if L`y'.treat_it == 1
  }

drop treat_lag

* Funding amounts and lags:

gen fund_now = hbfund_totinfladjust / ipop
replace fund_now=0 if year>1971
  forvalues y=1/20  {
	gen fund_lag`y' = L`y'.hbfund_totinfladjust / L`y'.ipop
  	replace fund_lag`y' = 0 if fund_lag`y' ==.
  }

gen fund_lag21 = 0
  forvalues y=21/30  {
	replace fund_lag21 = fund_lag21 + L`y'.hbfund_totinfladjust / L`y'.ipop if L`y'.hbfund_totinfladjust != .
  }

* Population controls:

gen ipop2=ipop/100000
gen pop65_pct=ipop65/ipop
gen poplt5_pct=ipoplt5/ipop

* MDs per capita:

gen infmdpc=infmd/(popul+1)

* Variable names:

label var ipop2 "Popn"
label var imedfaminc "Med. Fam Income"
label var pop65_pct "% Pop 65+"
label var poplt5_pct "% Pop <5"
label var infmdpc "NonFed MDs per Capita"
label var inwpop_pct "% Pop NonWh"

* Year dummies:

xi i.year

/* Below commands will drop 22 county/year observations with large swings (greater than 200 bed diff in dff in beds) and 0 beds as an observation */

gen diff2bd=abs(d2.beds_tot)
egen maxdiff2=max(diff2bd), by(fcounty)

bys fcounty : gen dropvar=1 if maxdiff2>200 & beds_tot==1 & beds_tot[_n-1]>1 & beds_tot[_n+1]>1  & year>=1948
replace dropvar=0 if missing(dropvar)
tab fcounty if dropvar==1

keep if dropvar==0 

* Terciles:

xtile quintile=ipop2 if year==1948, n(3)
tab quintile, sum (ipop2)
egen tercile=mean(quintile), by(fcounty)

****************************************************************************
* Table 5 Aggregate Effects on Hospital Capacity (using funding regression)*
****************************************************************************


*******************************************************
*******************************************************
*******************************************************
**** PREDICTIONS USING FUNDING REGS *******************

rename treat_it treat_lag0

areg bdpc treat_lag* fund_now fund_lag* _Iyear* ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct[aw=ipop2], absorb(fcounty) cluster(fcounty)

est store natl
preserve

* Baseline prediction:

predict basePC if year==1975 | year==1948, xbd

* County FEs:

predict ctyFE if year==1975, d

* Zero-out funding amounts and treatment indicators:

replace fund_now=0

forvalues i=1/21{
	replace fund_lag`i'=0
}

forvalues i=0/21{
	replace treat_lag`i'=0
}

* Counterfactual predictions:

predict simXB if year==1975 , xb

gen simlPC = ctyFE+simXB


* Average and total predictions:

summ basePC if year == 1948, d
summ basePC if year == 1975, d
summ simlPC, d

gen base_beds = basePC*(ipop/1000)
gen siml_beds = simlPC*(ipop/1000)

total base_beds if year == 1948
total base_beds if year == 1975
total siml_beds

* Check:

tab year if base_beds != .
tab year if siml_beds != .

summ base_beds, d
summ siml_beds, d

* Revert data:

restore



** Tercile 1 regression:

areg bdpc treat_lag* fund_now fund_lag* _Iyear* ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct if tercile==1 [aw=ipop2], absorb(fcounty) cluster(fcounty)

est store ter1
preserve

* Baseline prediction:

predict basePC if e(sample) & (year==1975 | year==1948), xbd

* County FEs:

predict ctyFE if year==1975 & e(sample), d

* Zero-out funding amounts and treatment indicators:

replace fund_now=0

forvalues i=1/21{
	replace fund_lag`i'=0
}

forvalues i=0/21{
	replace treat_lag`i'=0
}

* Counterfactual predictions:

predict simXB if year==1975 & e(sample), xb

gen simlPC = ctyFE+simXB

* Average and total predictions:

summ basePC if year == 1948, d
summ basePC  if year == 1975, d
summ simlPC , d

gen base_beds = basePC*(ipop/1000)
gen siml_beds = simlPC*(ipop/1000)

total base_beds if year == 1948
total base_beds if year == 1975
total siml_beds

* Check:

tab year if base_beds != .
tab year if siml_beds != .

summ base_beds, d
summ siml_beds, d

* Revert data:

restore

***

** Tercile 2 regression:

areg bdpc treat_lag* fund_now fund_lag* _Iyear* ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct if tercile==2 [aw=ipop2], absorb(fcounty) cluster(fcounty)

est store ter2
preserve

* Baseline prediction:

predict basePC if e(sample) & (year==1975 | year==1948), xbd

* County FEs:

predict ctyFE if year==1975 & e(sample), d

* Zero-out funding amounts and treatment indicators:

replace fund_now=0

forvalues i=1/21{
	replace fund_lag`i'=0
}

forvalues i=0/21{
	replace treat_lag`i'=0
}

* Counterfactual predictions:

predict simXB if year==1975 & e(sample), xb

gen simlPC = ctyFE+simXB

* Average and total predictions:

summ basePC [aw=ipop2] if year == 1948, d
summ basePC [aw=ipop2] if year == 1975, d
summ simlPC [aw=ipop2], d

gen base_beds = basePC*(ipop/1000)
gen siml_beds = simlPC*(ipop/1000)

total base_beds if year == 1948
total base_beds if year == 1975
total siml_beds

* Check:

tab year if base_beds != .
tab year if siml_beds != .

summ base_beds, d
summ siml_beds, d

* Revert data:

restore

***

** Tercile 3 regression:

areg bdpc treat_lag* fund_now fund_lag* _Iyear* ipop2 imedfaminc pop65_pct poplt5_pct infmdpc inwpop_pct if tercile==3 [aw=ipop2], absorb(fcounty) cluster(fcounty)

est store ter3
preserve

* Baseline prediction:

predict basePC if e(sample) & (year==1975 | year==1948), xbd

* County FEs:

predict ctyFE if year==1975 & e(sample), d

* Zero-out funding amounts and treatment indicators:

replace fund_now=0

forvalues i=1/21{
	replace fund_lag`i'=0
}

forvalues i=0/21{
	replace treat_lag`i'=0
}

* Counterfactual predictions:

predict simXB if year==1975 & e(sample), xb

gen simlPC = ctyFE+simXB

* Average and total predictions:

summ basePC [aw=ipop2] if year == 1948, d
summ basePC [aw=ipop2] if year == 1975, d
summ simlPC [aw=ipop2], d

gen base_beds = basePC*(ipop/1000)
gen siml_beds = simlPC*(ipop/1000)

total base_beds if year == 1948
total base_beds if year == 1975
total siml_beds

* Check:

tab year if base_beds != .
tab year if siml_beds != .

summ base_beds, d
summ siml_beds, d

* Revert data:

restore

